package com.yanqu.road.dao.impl.zhaoshang;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.zhaoshang.ZsUserData;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ZsUserDataDaoImpl extends TempDataObjectDao<ZsUserData> {


    @Override
    protected boolean addData(ZsUserData data) {
        PreparedStatement call = null;
        String sql = "insert into t_u_zs_week_data(userId, resetTime, type, weekToken,worShipTimes) values (?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setLong(i++, data.getResetTime());
            call.setInt(i++, data.getType());
            call.setLong(i++, data.getWeekToken());
            call.setInt(i++, data.getWorShipTimes());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(ZsUserData data) {
        PreparedStatement call = null;
        String sql = "update t_u_zs_week_data set resetTime=?, weekToken=?, worShipTimes=? where userId=? and type = ?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getResetTime());
            call.setLong(i++, data.getWeekToken());
            call.setInt(i++, data.getWorShipTimes());
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getType());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected ZsUserData parseData(ResultSet rs) throws SQLException {
        ZsUserData data = new ZsUserData();
        data.setUserId(rs.getLong("userId"));
        data.setResetTime(rs.getLong("resetTime"));
        data.setWeekToken(rs.getLong("weekToken"));
        data.setWorShipTimes(rs.getInt("worShipTimes"));
        data.setType(rs.getInt("type"));
        return data;
    }

    public Map<Integer, ZsUserData> getUserZsData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, ZsUserData> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_u_zs_week_data where userId = ?";
        try {
            int i = 1;
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                ZsUserData data = parse(rs);
                dataMap.put(data.getType(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }
}
